package cn.ym.mall.service.impl;

import cn.ym.mall.dao.ConsigneeInformationDao;
import cn.ym.mall.dao.impl.ConsigneeInformationDaoImpl;
import cn.ym.mall.entity.ConsigneeInformation;
import cn.ym.mall.service.ConsigneeInformationService;
import cn.ym.mall.utils.DruidUtils;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class ConsigneeInformationServiceImpl implements ConsigneeInformationService {
    private ConsigneeInformationDao consigneeInformationDao = new ConsigneeInformationDaoImpl();
    /**
     * 增加收件信息表
     *
     * @param consigneeInformation
     * @return
     */
    @Override
    public boolean addConsigneeInformation(ConsigneeInformation consigneeInformation) {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if(consigneeInformationDao.addConsigneeInformation(consigneeInformation) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 修改收件信息表
     *
     * @param consigneeInformation
     * @return
     */
    @Override
    public boolean updateConsigneeInformation(ConsigneeInformation consigneeInformation) {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if(consigneeInformationDao.updateConsigneeInformation(consigneeInformation) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 通过地址编号查询收件信息
     *
     * @param cinfoid
     * @return
     */
    @Override
    public ConsigneeInformation getConsigneeInformationBycinfoid(Integer cinfoid) {
        Connection conn = null;
        ConsigneeInformation consigneeInformation = null;
        try {
            conn = DruidUtils.getConnection();
            consigneeInformation = consigneeInformationDao.getConsigneeInformationBycinfoid(cinfoid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过cinfoid删除信息
     *
     * @param cinfoid
     * @return
     */
    @Override
    public boolean deleteConsigneeInformationByinfoid(Integer cinfoid) {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if(consigneeInformationDao.deleteConsigneeInformationByinfoid(cinfoid) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 查询收件信息表
     *
     * @return
     */
    @Override
    public List<ConsigneeInformation> getAllConsigneeInformation() {
        Connection conn = null;
        List<ConsigneeInformation> consigneeInformationList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection();
            consigneeInformationList = consigneeInformationDao.getAllConsigneeInformation();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
